const app = getApp();
import Api from '@/services/api.js'
const mapping = {
  bossPid: '1732542867481MoV1',
  hrPid: '17325391542597O0o'
}
export default {
  data() {
    return {
      tabActive: 0,
      primaryColor: app.globalData.PRIMARY_COLOR,
      tabs: [],
      list: [], // 数据列表
      inviteId: '', // 邀请id
    }
  },
  computed: {
    store() {
      return this.$store.state;
    },
    privacyData() {
      return this.store.privacyData;
    },
    isBoss() {
      return this.privacyData.roles.includes('boss')
    }
  },
  onLoad(options) {
    this.inviteId = options.inviteId;
  },
  onShow() {
    if (this.privacyData.openId) {
      this.init()
    }else {
      app.globalData.callBack = () => {
        this.init();
      }
    }
  },
  onShareAppMessage() {
    return {
      title: '人才匹配度测评工具',
      path: 'pages/home/index?inviteId=' + this.privacyData.userId,
      imageUrl: 'https://talentsupload.oss-cn-beijing.aliyuncs.com/miniprogram/default_2.png'
    }
  },
  methods: {
    async init() {
      await this.getTypeList();
      await this.getContentByTypeId();
      // 如果有邀请人id 就调用绑定的接口
      if (this.inviteId) {
        await this.$Serv.userBindByInviterId({ inviterId: this.inviteId })
      }
    },
    tapItem(item) {
      uni.navigateTo({
        url: '../common/description/index?content=' + encodeURIComponent(JSON.stringify(item))
      })
    },

    // 获取类型id
    async getTypeList() {
      const {
        code,
        result
      } = await this.$request({
        url: Api.getTypesByPid,
        data: {
          pid: this.isBoss ? mapping['bossPid'] : mapping['hrPid']
        }
      });
      if (code === 200) {
        this.tabs = result;
        this.tabActive = result[0]; // 默认展示第一条数据
      }
    },
    // 根据类别id查询内容
    async getContentByTypeId() {
      const {
        code,
        result
      } = await this.$request({
        url: Api.getHomeContentsByRoleId,
        data: {
          typeId: this.tabActive.id,
          pageNo: 1,
          pageSize: 50,
          contentType: 1,
          // roleId: 'all'
        }
      });
      if (code === 200) {
        this.list = result.records;
      }
    },
    handleTab(e) {
      const {
        item
      } = e.currentTarget.dataset;
      this.tabActive = item;
      this.getContentByTypeId();
    }
  }
}